$(document).ready(
		function() {
			var textarea = $("#wall-post textarea");
			textarea.click(function() {
				if ($("#wall-post-submit").button("option", "disabled")) {
					this.value = "";
				}
			});
			textarea.keyup(function() {
				if (this.value.length == 0) {
					$("#wall-post-submit").button("option", "disabled", true);
				} else {
					$("#wall-post-submit").button("option", "disabled", false);
				}
			});

			var post_button = $("#wall-post-submit");
			$("#wall-post-submit").button({
				disabled : true
			});

			$("#wall-post-submit").click(
					function() {
						if (!$("#wall-post-submit")
								.button("option", "disabled")) {
							$.post(this.form.action, $("#news-post-form")
									.serialize(), function(data) {
								if (data.message) {
									$("#wall-post-errors").html(data.message);
								} else {
									$("#wall-post-errors").html("");
									if ($("#wall-no-posts").is(":visible")) {
										$("#wall-no-posts").hide();
									}
									$(data.contents).hide().prependTo(
											"#wall-posts").fadeIn();
									$("textarea").val("");
									$("#wall-post-submit").button("option",
											"disabled", true);
								}
							});
						}
						return false;
					});
		});

$(document).ready(function() {
	var height = 45; // Minimum height of the box.
	$("#news-most-popular-tasks").children().each(function(index, value) {
		if ($(value).height() > height) {
			height = $(value).height() + 30;
		}
	});

	$("#news-most-popular-tasks").cycle({
		height : "" + height + "px"
	});
})

$(document).ready(
		function() {
			$("#event-code-submit").click(
					function() {
						$.post("/activities/attend_code/",
								$("#event-code-form").serialize(), function(
										data) {
									if (data.message) {
										$("#event-code-errors").html(
												data.message);
										$("#event-code-errors").show();
										if (data.social_email) {
											$("#social_email").show();
										}
									} else {
										window.location.replace("/activities/"
												+ data.type + "/" + data.slug
												+ "/");
									}
								});

						return false;
					});

			$("#social_email input").click(function() {
				this.value = "";
			});

		})

FB.init({
	appId : '233999643296575',
	status : true,
	cookie : true,
	xfbml : true
});

// Normally, we would wrap this in an anonymous function, but we want it to be
// available for later.
// Get the spreadsheet we need.
var energyLastThirtyDays = 'https://spreadsheets.google.com/spreadsheet/tq?key=0An9ynmXUoikYdHhxeW1xRURQZUlGd1oxVERnQktsWXc';
var refreshInterval = 0; // don't refresh this spreadsheet.

// Load the visualization API and the spreadsheet, then call displayRank.
var loader = new org.wattdepot.gdata.GDataLoader(displayRank,
		energyLastThirtyDays, refreshInterval);

function displayRank(energyDataTable) {
	// The source for which we want to find the rank must be specified by
	// Makahiki.
	var source = 'Ilima-E';
	var energyRank = new org.wattdepot.gdata.makahiki.EnergyRank();
	// Get the usage for the current round and overall.
	// console.log('Overall start: 87, end: 67');
	var usage = energyRank.processDataTable(energyDataTable, 87, 67);
	var info = energyRank.calculateRankInfo(usage, source);
	// Check if this is overall information.
	energyRank.draw("overall-energy-rank", "overall-energy-value", info);
	// console.log('Round 2 start: 80, end: 74');
	var usage = energyRank.processDataTable(energyDataTable, 80, 74);
	var info = energyRank.calculateRankInfo(usage, source);
	// Check if this is overall information.
	// Check if this is info for the current round.
	// console.log('Round 1 start: 87, end: 81');
	var usage = energyRank.processDataTable(energyDataTable, 87, 81);
	var info = energyRank.calculateRankInfo(usage, source);

	// Check if this is overall information.
	// Check if this is info for the current round.
};